package com.zzptc.easyshop.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONStreamAware;
import com.zzptc.easyshop.db.IDB;
import com.zzptc.easyshop.db.MysqlDB;
import com.zzptc.easyshop.vo.Users;
import com.zzptc.easyshop.vo.VO;

import javax.jws.soap.SOAPBinding;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ListResourceBundle;

public class UserDao1 implements IDAO{
    @Override
    public boolean insert(VO vo){
        MysqlDB db = new MysqlDB();
        PreparedStatement ps= null;
        Connection conn = null;
        int i = 0;
        try {
            ps = db.getConn().prepareStatement("select * from easybuy_user(eu_user_name,eu_password)values(?,?)");
            Users users = (Users) vo;
            ps.setString(1,users.getEu_user_name());
            ps.setString(2,users.getEu_password());
            i = ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                db.closeAll(conn,ps,null);
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


        return i>0;
    }

    @Override
    public boolean login(VO vo) throws SQLException {
        MysqlDB db = new MysqlDB();
        PreparedStatement ps = null;
        Connection conn = null;
        ResultSet rs = null;
         boolean i = false;
        try {
            ps = db.getConn().prepareStatement("select * from easybuy_user where eu_user_id=? and eu_password=?");
            Users users = (Users) vo;
            ps.setString(1, users.getEu_user_name());
            ps.setString(2, users.getEu_password());

            rs = ps.executeQuery();
            i = rs.next();
        }catch(SQLException e){
            e.printStackTrace();

        }finally {
            try {
            db.closeAll(conn,ps,rs);
            ps.close();
            rs.close();
            }catch(SQLException e){
                e.getSQLState();
            }
        }
        return i;
    }

    @Override
    public List getAll() throws SQLException {
        List<Users> list = new ArrayList<Users>();
        IDB db = new MysqlDB();
        Connection conn= null;
        PreparedStatement ps =null;
        ResultSet rs = null;
        try {
            conn = db.getConn();
            String sql = "select * from easybuy_user";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            while (rs.next()) {
                Users users = new Users();
                users.setEu_user_name(rs.getString("Eu_user_name"));
                users.setEu_password(rs.getString("Eu_password"));
                users.setEu_address(rs.getString("Eu_address"));
                users.setEu_birthday(rs.getString("Eu_birthday"));
                users.setEu_email(rs.getString("Eu_email"));
                users.setEu_identity_code(rs.getString("Eu_identity_code"));
                users.setEu_mobile(rs.getString("Eu_mobile"));
                users.setEu_sex(rs.getString("Eu_sex"));
                users.setEu_status(rs.getInt("Eu_status"));
                users.setEu_user_id(rs.getString("Eu_user_id"));
                list.add(users);
            }
        }catch (SQLException e){
          e.printStackTrace();
        }finally {
            try {
                db.closeAll(conn,ps,rs);
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        return list;
    }
    public static void main(String[] args) {
     UserDao userDao = new UserDao();
        List<VO> listall = (ArrayList)userDao.getAll();
        String j = JSON.toJSONString(listall);
       System.out.println(j);
       List<Users> text = JSONObject.parseArray(j,Users.class);
        System.out.println(text.size());

    }
}
